/*   The MIT License
 *   
 *   Carina Maya Exporter
 *   Copyright (c) 2010-2011 Zdravko Velinov
 *   
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 */

#include "ces-exporter.hh"
#include "maya-config.hh"

#include "carina/common/system.hh"

#include <maya/MGlobal.h>
#include <maya/MLibrary.h>
#include <maya/MFileIO.h>

int main(int argc, char** argv)
{
    string maya_location;
    if(!Carina::System::getEnvironmentVariable("MAYA_LOCATION", maya_location))
        if(!Carina::System::setEnvironmentVariable("MAYA_LOCATION", MAYA_ROOT_DIR))
            return EXIT_FAILURE;

    MStatus err = MLibrary::initialize(argv[0], true);
    CHECK_MSTATUS_AND_RETURN(err, EXIT_FAILURE);

//*
    if(argc < 3)
        return 1;

    const char* input_filename = argv[1];
    const char* output_filename = argv[2];
/*/
    const char* input_filename = "C:/Users/ZE/workspace/Carina/source/carina_engine/share/resources/torus.mb";
    const char* output_filename = "C:\\Users\\ZE\\workspace\\Carina\\source\\carina_engine\\share\\resources\\test_torus.ces";
//*/

    MFileIO::newFile(true);

    err = MFileIO::open(input_filename);
    CHECK_MSTATUS_AND_RETURN(err, EXIT_FAILURE);

    err = MGlobal::executeCommand("delete -ch");
    CHECK_MSTATUS_AND_RETURN(err, EXIT_FAILURE);

    CarinaMayaTranslator exporter;
    MFileObject output_file;
    err = output_file.setRawFullName(output_filename);
    CHECK_MSTATUS_AND_RETURN(err, EXIT_FAILURE);

    err = exporter.writer(output_file, "",  MPxFileTranslator::kExportAccessMode);
    CHECK_MSTATUS_AND_RETURN(err, EXIT_FAILURE);

    MLibrary::cleanup();

    return EXIT_SUCCESS;
}